﻿using System;
using System.Collections.Generic;
using System.Text;
using Gx.Common;
using Gx.Dal.Shared;
using Gx.Model;
using Gx.Model.Entity;
using Gx.Model.Enums;

namespace Gx.Bll.Manage
{
    /// <summary>
    /// 配置逻辑层
    /// </summary>
    public class M_DictInfoBll
    {
        public List<M_DictInfo> GetDictListTotal()
        {
            List<M_DictInfo> dictList = new List<M_DictInfo>
            {
                new M_DictInfo() { DictValue = "0", DictName = "全部" },
                new M_DictInfo() { DictValue = ((int)DictEnmus.Total).ToString(), DictName = EnumHelper.GetDataTypeDescription<DictEnmus>(DictEnmus.Total.ToString()) }
            };
            dictList.AddRange(GetDictInfoList(new List<int>() { (int)DictEnmus.Total }));
            return dictList;
        }


        public List<M_DictInfo> GetDictInfoList(int dictType)
        {
            using (DictInfoDal dal = new DictInfoDal())
            {
                return dal.GetM_DictInfoList(new List<int>() { dictType });
            }
        }
        /// <summary>
        /// 根据ID获取配置数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public List<M_DictInfo> GetDictInfoList(List<int> dictType)
        {
            using (DictInfoDal dal = new DictInfoDal())
            {
                return dal.GetM_DictInfoList(dictType);
            }
        }

        /// <summary>
        /// 分页获取配置信息列表
        /// </summary>
        /// <param name="pageIndex">索引</param>
        /// <param name="pageSize">数据条数</param>
        /// <param name="dictType">类型</param>
        /// <param name="TotalCount">总计数</param>
        /// <returns>List<DictInfo></returns>
        public List<M_DictInfo> GetM_DictInfoList(int pageIndex, int pageSize, int? dictType, out int totalCount)
        {
            using (DictInfoDal dal = new DictInfoDal())
            {
                int PageIndex = (pageIndex - 1) * pageSize + 1;
                int PageSize = pageIndex * pageSize;
                StringBuilder strWhre = new StringBuilder();
                strWhre.AppendFormat(" AND Status=0");
                if (dictType != null && dictType > 0)
                {
                    strWhre.AppendFormat(" AND DictType={0}", dictType);
                }
                return dal.GetDictInfoList(strWhre.ToString(), PageIndex, PageSize, out totalCount);
            }
        }

        /// <summary>
        /// 新增一条配置数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnModel AddDictInfo(M_DictInfo model)
        {
            using (DictInfoDal dal = new DictInfoDal())
            {
                ReturnModel r = new ReturnModel()
                {
                    Msg = "操作失败",
                };
                bool res = dal.AddDictInfo(model);
                if (res)
                {
                    new M_LogInfoBll().AddOptionsLog("AddDictInfo", "manage新增一条配置信息", model);
                    r.Msg = "操作成功";
                    r.Success = true;
                }
                return r;
            }
        }

        /// <summary>
        /// 编辑一条配置数据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ReturnModel EditDictInfo(M_DictInfo model)
        {
            using (DictInfoDal dal = new DictInfoDal())
            {
                ReturnModel r = new ReturnModel()
                {
                    Msg = "操作失败",
                };
                bool res = dal.EditDictInfo(model);
                if (res)
                {
                    new M_LogInfoBll().AddOptionsLog("EditDictInfo", "manage编辑一条配置信息", model);
                    r.Code = (int)Code.操作成功;
                    r.Msg = "操作成功";
                    r.Success = true;
                }
                return r;
            }
        }

        /// <summary>
        /// 删除一条配置信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ReturnModel DelDictInfo(int id)
        {
            using (DictInfoDal dal = new DictInfoDal())
            {
                ReturnModel r = new ReturnModel()
                {
                    Msg = "操作失败",
                };
                bool res = dal.DelDictInfo(id);
                if (res)
                {
                    new M_LogInfoBll().AddOptionsLog("DelDictInfo", "manage_" + LoginHelper.GetUserId() + "删除编辑一条配置信息", id);
                    r.Msg = "操作成功";
                    r.Success = true;
                }
                return r;
            }
        }
    }
}
